package com.blb.httptest;

import java.io.*;

/**
 * IO操作工具类
 */
public class IOUtils {

    /**
     * 流字节数据的复制
     * @param input
     * @param output
     */
    public static void copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024];
        int length = 0;
        //一边从输入流读取字节
        while((length = input.read(buffer)) != -1){
            //一边写入到输出流
            output.write(buffer,0,length);
        }
        output.flush();
    }

    /**
     * 读取流中的文字内容
     * @param input
     * @return
     * @throws IOException
     */
    public static String readString(InputStream input) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        byte[] buffer = new byte[1024];
        //一边从输入流读取字节
        while(true){
            int length = input.read(buffer);
            //一边写入到输出流
            String string = new String(buffer,0,length);
            stringBuilder.append(string);
            System.out.println(length);
            if(length < 0){
                break;
            }
        }
        return stringBuilder.toString();
    }
}
